import { Injectable } from '@angular/core';
import { AlertController, LoadingController, Platform, ToastController } from '@ionic/angular';
import { Logger } from './Logger';
import { environment } from '../../environments/environment';
import { Storage } from './Storage';
import * as constants from './Constants'

/**
 * 帮助类：存放和业务有关的公共方法
 */
@Injectable({
    providedIn: 'root'
})
export class Helper {
    readonly IsMobile: boolean = false;
    private AlertIsExist = false;
    private LoadingIsExist = false;
    private Loading = null;
    loading: any = null;
    loadingsController: any = null;
    public loadingIsOpen: any = false;
   
    constructor(public platform: Platform,
        public alertController: AlertController,
        public loadingController: LoadingController,
        public toastController: ToastController) {
        this.IsMobile = this.platform.is('cordova');
    }


    /**
     * 是否真机环境
     */
    isMobile(): boolean {
        return this.IsMobile;
    }

    /**
     * 是否真机环境
     */
    isNotMobile(): boolean {
        return !this.isMobile();
    }


    /**
     * 是否android真机环境
     */
    isAndroid(): boolean {
        return this.isMobile() && this.platform.is('android');
    }

    /**
     * 是否ios真机环境
     */
    isIos(): boolean {
        return this.isMobile() && (this.platform.is('ios') || this.platform.is('ipad') || this.platform.is('iphone'));
    }


    /**
     * 断言是否真机环境
     */
    assertIsMobile(): void {
        if (this.isNotMobile()) {
            this.toast('请使用真机调试');
            throw new Error('请使用真机调试');
        }
    }

    /**
     * tip 开发中
     */
    tipDev() {
        this.toast('敬请期待');
    }

    /**
     * alert弹框，默认只有确定按钮，当存在取消回调函数则会显示取消按钮
     * 注：如果存在打开的alert则不再打开
     * @param header 需要显示的title
     * @param message 需要显示的内容
     * @param okBackFun 成功回调
     * @param cancelBtnFun 失败回调
     */
    alert(header = '', message = '', okBackFun: any = null, cancelBtnFun = null): void {
        // alertController.create是异步方法，所以使用AlertIsExist标志是否打开
        this.hideLoading();
        if (this.AlertIsExist) {
            Logger.log('alert已经存在，禁止重复打开');
            setTimeout(() => { // alert关闭的可能性比较多，不止点击确定或取消按钮
                this.AlertIsExist = false;
            }, 10000);
            return;
        }
        Logger.log('alert已经存在，禁止重复打开--------------------------------------');
        this.AlertIsExist = true;
        const buttons = [{
            text: 'Ok',
            handler: () => {
                this.AlertIsExist = false;
                okBackFun && okBackFun();
            }
        }];
        if (cancelBtnFun) {
            const cancelBtn = {
                text: 'Cancel',
                role: 'cancel',
                handler: () => {
                    this.AlertIsExist = false;
                    cancelBtnFun();
                }
            };
            buttons.unshift(cancelBtn);
        }
        this.alertController.create({
            header,
            
            message,
            buttons
        }).then(alert => alert.present());
    }

    /**
     * alert弹框， 自定义确定按钮  默认只有确定按钮，当存在取消回调函数则会显示取消按钮
     * 注：如果存在打开的alert则不再打开
     * @param header 需要显示的title
     * @param message 需要显示的内容
     * @param okBackFun 成功回调
     * @param cancelBtnFun 失败回调
     */
    alertBtn(header = '', message = '', okBackFun: any = null, cancelBtnFun = null,okBtn:string='Ok',okCancel:string='Cancel'): void {
        // alertController.create是异步方法，所以使用AlertIsExist标志是否打开
        this.hideLoading();
        if (this.AlertIsExist) {
            Logger.log('alert已经存在，禁止重复打开');
            setTimeout(() => { // alert关闭的可能性比较多，不止点击确定或取消按钮
                this.AlertIsExist = false;
            }, 10000);
            return;
        }
        Logger.log('alert已经存在，禁止重复打开--------------------------------------');
        this.AlertIsExist = true;
        const buttons = [{
            text: okBtn,cssClass:'alert-dialog-ok', handler: () => {
                this.AlertIsExist = false;
                okBackFun && okBackFun();
            }
        }];
        if (cancelBtnFun) {
            const cancelBtn = {
                text:okCancel,
                cssClass:'alert-dialog-cancel',  
                role: 'cancel',
                handler: () => {
                    this.AlertIsExist = false;
                    cancelBtnFun();
                }
            };
            buttons.unshift(cancelBtn);
        }
        this.alertController.create({
            header,
        
            message,
            buttons
        }).then(alert => alert.present());
    }
    /**
     * 显示提示信息
     * 建议优先调用 NativeService.toast
     */
    toast(message: string = 'Message', duration: number = 1500, position: 'top' | 'bottom' | 'middle' = 'middle'): void {
        // const opts = {message, duration, color: '', position, showCloseButton: true, closeButtonText: '✖'};
        const opts = { message, duration, color: '', position, };
        this.toastController.create(opts).then(toast => toast.present());
    }


    /**
       * 统一调用此方法显示loading
       */
    async  showLoadingMsg(message: string = '') {
        this.loadingIsOpen = true;
        return await this.loadingController.create({
            duration: 7000,
        }).then(a => {
            a.present().then(() => {
                if (!this.loadingIsOpen) {
                    a.dismiss()
                }
            });
        });
        // await this.loadingsController.present();
    }
    async hideLoadingMsg() {
        if (this.loadingIsOpen == true) {
            this.loadingIsOpen = false;
            return await this.loadingController.dismiss();
        }
        // console.log('this.loadingsController===',this.loadingsController);
        // if (this.loadingsController) {
        //     await this.loadingsController.dismiss();
        // }
    }


    /**
     * 统一调用此方法显示loading
     */
    showLoading(message: string = ''): void {
        console.log('重新加载一次');
        if (this.LoadingIsExist) {
            return;
        }
        if (this.Loading) {
            this.Loading.dismiss();
        }
        this.LoadingIsExist = true;
        this.Loading = this.loadingController.create({
            spinner: 'bubbles', // dots https://ionicframework.com/docs/api/spinner
            duration: 20000,
            message
        }).then(loading => {
            // loadingController.create异步方法，调用loading.present()前有可能已经调用hideLoading方法
            if (this.LoadingIsExist) {
                console.log('加载loading------------------------', this.LoadingIsExist);
                this.Loading.present();
            } else {
                this.Loading.dismiss();
            }
        });
    }

    /**
     * 关闭loading
     */
    // hideLoading(): void {
    async hideLoading() {
        // if (this.loading) {
        //     console.log('关闭加载loading------------------------',this.loading);
        //       this.loading.dismiss();
        // }
        this.LoadingIsExist = false;
        if (this.Loading) {
            console.log('关闭');
            this.Loading.dismiss();
            this.Loading = null;
        }
    }




}
